package com.ssish.saas.eccs.util;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import java.util.Random;

public class MD5Util {
	private static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	private static final String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if ((charsetname == null) || ("".equals(charsetname)))
				resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
		} catch (Exception localException) {
		}
		return resultString.toUpperCase();
	}
	
	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}
	
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}
	
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;

		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}
	
	public static String GetMD5nonce_str() {
		Random rnd = new Random();
		int num = rnd.nextInt(899999999) + 1000000;
		String strObj = String.valueOf(num);
		String resultString = null;
		try {
			resultString = new String(strObj);
			MessageDigest md = MessageDigest.getInstance("MD5");

			resultString = byteToString(md.digest(strObj.getBytes()));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}
	
	private static String byteToString(byte[] bByte) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bByte.length; i++) {
			sBuffer.append(byteToArrayString(bByte[i]));
		}
		return sBuffer.toString();
	}
	
	public static String nonceStrNumber() {
		Random rnd = new Random();
		int num = rnd.nextInt(899999999) + 100000000;
		return String.valueOf(num);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(encrypt());
	}
	public static String encrypt() throws Exception {
		try {
			String pstJson = "[{\n\"Key1\":\"Var1\",\n\"Key2\":\"Var2\",\n\"Key3\":\"Var3\",\n\"Key4\":\"Var4\"\n}]";
			System.out.println(pstJson);
			String parKey = "rZcXb31579246275";
			String pstIV = "1579246275123456";

			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			int blockSize = cipher.getBlockSize();
			// int blockSize=256;

			byte[] dataBytes = pstJson.getBytes();
			int plaintextLength = dataBytes.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}


			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

			SecretKeySpec keyspec = new SecretKeySpec(parKey.getBytes(), "AES");
			IvParameterSpec ivspec = new IvParameterSpec(pstIV.getBytes());

			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			// cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			byte[] encrypted = cipher.doFinal(plaintext);


			return new sun.misc.BASE64Encoder().encode(encrypted);


		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*public String encrypt(String Data) throws Exception {
		try {
			byte[] iv = aiv.getBytes();//因为要求IV为16byte，而此处aiv串为32位字符串，所以将32位字符串转为16byte
			Cipher cipher = Cipher.getInstance(ALGO_MODE);
			int blockSize = cipher.getBlockSize();
			byte[] dataBytes = Data.getBytes();
			int plaintextLength = dataBytes.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}
			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
			SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
			IvParameterSpec ivspec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			byte[] encrypted = cipher.doFinal(plaintext);
			String EncStr = (new BASE64Encoder()).encode(encrypted);//将cipher加密后的byte数组用base64加密生成字符串
			return EncStr ;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}*/




}
